package com.ll.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import com.ll.bean.ManagerUserBean;
import com.ll.bean.ServiceUserBean;
import com.ll.util.MD5Security;
import com.ll.dao.DB;

public class LoginAction extends ActionSupport
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1768578672077922542L;
	private String username;
	private String password;
	
	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	@Override
	public String execute() throws Exception
	{
		ManagerUserBean managerUser = null;
		ServiceUserBean serviceUser = null;
		
		String result = null;
		
		// 开始写数据库
		Connection con = null;
		try
		{
			//查询管理员账户表
			con = DB.getConnection();
			PreparedStatement st = 
					con.prepareStatement("select username, upwd," +
							" company, uemail, province, city, phone" +
							" from t_manager_user where username=? and upwd=?");
			st.setString(1, username);
			st.setString(2, MD5Security.code(password));
			ResultSet rs = st.executeQuery();
			
			int role = -1;
			while(rs.next())
			{
				managerUser = new ManagerUserBean();
				
				managerUser.setUsername(rs.getString(1));
				managerUser.setUpwd(rs.getString(2));
				managerUser.setCompany(rs.getString(3));
				managerUser.setUemail(rs.getString(4));
				managerUser.setProvince(rs.getString(5));
				managerUser.setCity(rs.getString(6));
				managerUser.setPhone(rs.getString(7));
				
				role = 1;
			}
			
			//查询客服账户表
			if(role == -1)
			{
				st = con.prepareStatement("select * from t_service_user where username=? and password=?");
				st.setString(1, username);
				st.setString(2, MD5Security.code(password));
				rs = st.executeQuery();
				
				while(rs.next())
				{
					serviceUser = new ServiceUserBean();
					
					serviceUser.setUsername(username);
					serviceUser.setPassword(password);
					
					
					serviceUser.setDept(rs.getString(3));
					serviceUser.setName(rs.getString(4));
					serviceUser.setOfficePhone(rs.getString(5));
					serviceUser.setSex(rs.getString(6));
					serviceUser.setEmail(rs.getString(7));
					serviceUser.setQq(rs.getString(8));
					
					role = 2;
				}
			}
			
			if(role != -1)
			{	
				if(role == 1) //管理员登录
				{
					ActionContext.getContext().getSession().put("_manager_user", managerUser);
					result = "manager";
				}
				else if(role == 2) //客服登录
				{
					ActionContext.getContext().getSession().put("_service_user", serviceUser);
					result = "service";
				}
			}
			else
			{
				result = "error";
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			try {
				if(con != null)
					con.close();
				}catch(Exception e){
					e.printStackTrace();
				}
		}
		
		return result;
	}
	
}
